//
//  ViewController.m
//  Project
//
//  Created by  CarlsonLee on 2018/12/5.
//  Copyright © 2018年 YANJING. All rights reserved.
//

#import "ViewController.h"
#import "PScrollView.h"
#import "PSegmentViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    BOOL _tableCanScroll;
    BOOL _mainCanScroll;
}
@property (nonatomic, strong) PScrollView* scrollView;
@property (nonatomic, strong) PSegmentViewController* segment;

@end

@implementation ViewController

#pragma mark - ♻️life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view setBackgroundColor:ColorFromRGB(0xffffff)];
    _mainCanScroll = YES;
    [self p_initSubViews];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(segmentDidMoveToTop) name:@"segmentDidMoveToTop" object:nil];
}

#pragma mark - 🔒private
- (void)p_initSubViews
{
    self.scrollView = [PScrollView new];
    [self.view addSubview:self.scrollView];
    AdjustsScrollViewInsetNever(self, self.scrollView);
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = ColorFromRGB(0xa0a0a0);
    self.scrollView.contentSize = CGSizeMake(0, kScreen.height+150);
    
    self.segment = [PSegmentViewController new];
    [self.scrollView addSubview:self.segment.view];
    [self.segment didMoveToParentViewController:self];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(0);
        make.size.mas_equalTo(kScreen);
    }];
    [self.segment.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(150);
        make.left.mas_offset(0);
        make.size.mas_equalTo(CGSizeMake(kScreen.width, kScreen.height-150));
    }];
}

#pragma mark - 🔄overwrite

#pragma mark - 🚪public

#pragma mark - 🍐delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* identify = @"cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"cell_%ld", indexPath.row];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //设定一个滑动边界
    CGFloat bound_y = 100;
    CGFloat offset_y = scrollView.contentOffset.y;
    if(offset_y >= bound_y){
        _mainCanScroll = NO;
        self.segment.tableCanScroll = YES;
    }else{
        self.segment.tableCanScroll = NO;
    }
    
    if(!_mainCanScroll){
        self.scrollView.contentOffset = CGPointMake(0, bound_y);
    }
    CGFloat add_y = offset_y;
    if(add_y>bound_y || !_mainCanScroll)add_y=bound_y;
    [self.segment.view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kScreen.height-150+add_y);
    }];
    self.scrollView.showsVerticalScrollIndicator = _mainCanScroll;
}

- (void)segmentDidMoveToTop
{
    _mainCanScroll = YES;
    self.segment.tableCanScroll = NO;
}

#pragma mark - TableView Delegate and DataSource

#pragma mark - ☎️notification

#pragma mark - 🎬event response

#pragma mark - ☸getter and setter

@end
